package cn.t.converter.industry;

import cn.t.constants.CommonConstants;
import cn.t.model.device.CargowaySkuDO;
import cn.t.model.device.DeviceDO;
import cn.t.utils.DateUtil;
import cn.t.vo.industry.IndustryDeviceVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 设备转换器
 */
@Mapper(componentModel = "spring")
public interface IndustryDeviceConverter {

    @Mapping(target = "planQty", source = "cargowayVol")
    @Mapping(target = "stockStatus", expression = "java(calStockStatus(actQty, cargowaySku.getAlarmLimit()))")
    IndustryDeviceVO.Cargoway data2IndustryCargoway(CargowaySkuDO cargowaySku, int actQty, Integer cargowayVol);

    @Mapping(target = "deviceId", source = "deviceDO.id")
    @Mapping(target = "stockRate", source = "stockRate")
    @Mapping(target = "onlineStatus", expression = "java(calOnlineStatus(deviceDO.getGmtConnect()))")
    @Mapping(target = "deviceType", expression = "java(cn.t.enums.device.DeviceTypeEnum.getName(deviceDO.getDeviceType()))")
    IndustryDeviceVO data2IndustryDevice(DeviceDO deviceDO, String stockRate, int cargowayNum, List<IndustryDeviceVO.Cargoway> cargowayList);

    default String calStockStatus(int actQty, Integer alarmLimit) {
        if (0 == actQty) {
            return "缺货";
        } else if (alarmLimit != null && actQty < alarmLimit) {
            return "需补";
        } else {
            return "充足";
        }
    }

    default int calOnlineStatus(Date connectTime) {
        return connectTime == null || DateUtil.diffTime(connectTime, new Date(), TimeUnit.MINUTES) > CommonConstants.FIFTEEN ? CommonConstants.ZERO : CommonConstants.ONE;
    }
}

